package Servlets.lhl;/**
 * 项目名称：ProaddServlet.java
 * 文件名称：ProaddServlet.java
 * 类的描述：无
 * 创建时间：2025-07-26 11:50:04
 * 创建人：大帅比
 * <p>
 * 版权声明：本文件归 大帅比 所有，未经授权不得进行商业使用。
 */
 /*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            佛祖保佑       永无BUG
*/

import java.io.*;
import java.util.UUID;

import Util.GokJDBCUtil2;
import Util.RES;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@WebServlet(name = "ProaddServlet", value = "/proadd")
@MultipartConfig
public class ProaddServlet extends HttpServlet {

    public void init() {
    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
        String proname = req.getParameter("proname");
        String price = req.getParameter("price");
        String originplace = req.getParameter("originplace");
        String levelid = req.getParameter("levelid");
        String categoryid = req.getParameter("categoryid");
        String productionmonth = req.getParameter("productionmonth");
        String productionyear = req.getParameter("productionyear");
        String quantity = req.getParameter("quantity");
        String status = req.getParameter("status");
        String suppid = req.getParameter("suppid");
        Part image = req.getPart("image");
        String description = req.getParameter("description");
        //如果都不为null且有长度
        if (proname == null || proname.trim().isEmpty() ||
                price == null || price.trim().isEmpty() ||
                originplace == null || originplace.trim().isEmpty() ||
                levelid == null || levelid.trim().isEmpty() ||
                categoryid == null || categoryid.trim().isEmpty() ||
                productionmonth == null || productionmonth.trim().isEmpty() ||
                productionyear == null || productionyear.trim().isEmpty() ||
                quantity == null || quantity.trim().isEmpty() ||
                status == null || status.trim().isEmpty() ||
                suppid == null || suppid.trim().isEmpty() ||
                image == null || image.getSize() == 0 || image.getSubmittedFileName().isEmpty() ||
                description ==null || description.isEmpty()
        ) {
            res.getWriter().write(RES.fail("请填写完整"));
            return;
        }else{
            String sql="insert into product(proname,price,originplace,levelid,categoryid,productionmonth,productionyear,quantity,status,suppid,src,description) values(?,?,?,?,?,?,?,?,?,?,?,?)";
            String houzhui = image.getSubmittedFileName().substring(image.getSubmittedFileName().indexOf("."));
            String name = UUID.randomUUID().toString().replaceAll("-", "")+houzhui;
            String path=req.getContextPath()+"/pages/images/"+ name;
            image.write(req.getServletContext().getRealPath("/pages/images")+"/"+name);
            GokJDBCUtil2.update(sql, proname, price, originplace, levelid, categoryid, productionmonth, productionyear, quantity, status, suppid, path,description);
            res.getWriter().write(RES.success("添加成功"));
        }

    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        String method = req.getMethod();
        try {
            if (method.equals("POST")) {
                this.doPost(req, res);
            } else {
                PrintWriter writer = res.getWriter();
                writer.println("{\"code\":\"400\",\"msg\":\"此方法不允许\"}");
                writer.close();
            }
        } catch (Exception e) {
            log.error("服务器处理请求时发生错误", e);
            PrintWriter writer = res.getWriter();
            writer.println("{\"code\":\"500\",\"msg\":\"服务器处理请求时发生错误\"}");
            writer.close();
        }
    }

    public void destroy() {
    }
}